package cn.zlp.pet.controller;

import cn.zlp.basic.util.AjaxResult;
import cn.zlp.basic.util.PageList;
import cn.zlp.pet.domain.Pet;
import cn.zlp.pet.domain.PetDetail;
import cn.zlp.pet.query.PetQuery;
import cn.zlp.pet.service.IPetDetailService;
import cn.zlp.pet.service.IPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "宠物接口",description = "宠物接口详细描述")
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @Autowired
    private IPetDetailService petDetailService;

    //获取一个对象：get
    //@GetMapping相当于@RequestMapping(value="/xxx",method=RequestMethod.GET)
    //Get http://localhost:8080/pet/1
    @ApiOperation(value = "查询单个宠物",notes = "需要传递id")
    @GetMapping("/{id}")//  pet/1    或 /pet/2
    public Pet findById(@PathVariable("id")Long id){
        return petService.findById(id);
    }

    //获取一个对象：get
    //@GetMapping相当于@RequestMapping(value="/xxx",method=RequestMethod.GET)
    //Get http://localhost:8080/pet/1
    @ApiOperation(value = "根据宠物id查询宠物明细",notes = "需要传递id")
    @GetMapping("/detail/{id}")//  pet/1    或 /pet/2
    public PetDetail findPetDetailById(@PathVariable("id")Long id){
        return petDetailService.getByPetId(id);
    }


    //获取所有对象：get
    //Get http://localhost:8080/pet
    @ApiOperation(value = "查询所有",notes = "不需参数")
    @GetMapping
    public List<Pet> findAll(){
        return petService.findAll();
    }


    //分页 + 高级：post - 直接写，后面发送异步请求时再说
    //请求数据包中：请求行 + 请求头 + 请求体【参数就在请求体中】
    //Post http://localhost:8080/pet + json参数petQuery
    @ApiOperation(value = "分页查询宠物信息",notes = "需要传递当前页和每页显示的条数")
    @PostMapping
    public PageList<Pet> findPage(@RequestBody PetQuery petQuery){
        return petService.pageQuery(petQuery);
    }

    /** 宠物删除 */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "宠物删除接口",notes = "需要传入参数id")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            petService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("删除失败");
        }
    }


    /** 宠物批量删除 */
    @PatchMapping
    @ApiOperation(value = "宠物批量删除接口",notes = "需要传入参数id集合")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            petService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /** 宠物修改和新增 */
    @PutMapping
    @ApiOperation(value = "宠物修改和新增接口",notes = "需要传入登录信息参数")
    public AjaxResult saveOrUpdate(@RequestBody Pet pet,HttpServletRequest request){
        try {
            if (pet.getId() == null) {
                petService.add(pet,request);
            }else {
                // 调用update方法
                petService.update(pet);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("操作失败");
        }
    }

    @PostMapping("/onsale")
    @ApiOperation(value = "宠物上线接口",notes = "需要传入上线id")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("操作失败");
        }
    }

    @PostMapping("/offsale")
    @ApiOperation(value = "宠物下线接口",notes = "需要传入下线id")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("操作失败");
        }
    }

    /**
     * 宠物领养
     * @param map
     * @param request
     * @return
     */
    @PostMapping("/adopt")
    @ApiOperation(value = "宠物领养接口",notes = "需要传入领养的id")
    public AjaxResult adopt(@RequestBody Map<String,Long> map, HttpServletRequest request){
        try {
            Long id = map.get("id");
            petService.adopt(id,request);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("操作失败");
        }
    }

    /**
     * 是否为店铺管理员
     * @param request
     * @return
     */
    @PostMapping("/isShopAdmin")
    @ApiOperation(value = "判断是否为店铺管理员接口",notes = "需要传入请求对象HttpServletRequest")
    public AjaxResult isShopAdmin(HttpServletRequest request){
        try {
            petService.isShopAdmin(request);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false);
        }
    }

}
